#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   old_api_result_check_test.py
@Contact :   fengfeng.qiu@amh-group.com

@Modify Time      @Author    
------------      -------    
2022/4/20 10:54   qiufengfeng      
'''
import pandas as pd
import json
import requests
import ast


def load_test_data(path):
    def processo_data(item):
        json_obj = ast.literal_eval(item['asr_detail'])
        result = []
        for i in json_obj:
            result.append(i['role'] + ":" + i["text"])
        return result
    df = pd.read_csv(path,'')[1980:]
    df.dropna(subset=["asr_detail"],inplace=True)
    df["dialogue"] = df.apply(processo_data,axis=1)
    return df["dialogue"].tolist(),df["airesponse"].tolist()


def http_post(url,dialogue_list):

    post_body = {"dialogue": [dialogue_list]}
    headers = {
        'Content-Type':"application/json"
    }
    success = 0
    output = None
    while success != 1:
        try:
            req = requests.post(url,data=json.dumps(post_body),headers=headers)
            output  = req.json()
            success = 1
        except Exception as e :
            print("try again")
            success = 0
    return output

def api_result(dialogue):
    beetle_url = "https://dev-predict-phantom.amh-group.com/contraband/v1/models/contraband:predict"

    beetle_result = http_post(beetle_url,dialogue)

    return beetle_result


def check_list_same(list_one,list_two):
    if len(list_one) != len(list_two):
        return False

    for item in list_one:
        if item not in list_two:
            return False
    return True

def check_list_same(list_one,list_two):
    if len(list_one) != len(list_two):
        return False

    for item in list_one:
        if item not in list_two:
            return False
    return True



def __main__():
    data_path = './data/风控数据.csv'
    dialogues,origin_json_strs = load_test_data(data_path)
    for index, (dialogue,origin_json_str) in enumerate(zip(dialogues,origin_json_strs)):
        beetle_json = api_result(dialogue)
        origin_json = json.loads(origin_json_str)
        if check_list_same(origin_json["contraband_infos"][0]["sure_contraband"],beetle_json["contraband_infos"][0]["sure_contraband"]) :
            if round(origin_json["contraband_infos"][0]["sure_contraband_prob"]) == round(beetle_json["contraband_infos"][0]["sure_contraband_prob"]):
                print("ok")
        else:
            print(index)
            print(dialogue)
            print("两次结果不一样")

if __name__ == '__main__':
    __main__()